import Vue from "vue";
import Router from "vue-router"
import Home from '../views/Home'
import Reg from '../views/Reg'
import login from '../views/login'
import main from '../views/main'
import kind from '../views/kind'
import quickReg from '../views/quickReg'
import goods from '../views/goods'
import goodscart from '../views/goodscart'
import Notfound from '../views/Notfound'
import regnew from '../views/regnew'
import pass from '../views/pass'
import search from '../views/search'
import searchList from '../views/searchList'
import setup from '../views/setup'


// 把插件安装到Vue里
Vue.use(Router)
let router = new Router({
    method:'history',
   routes:[
        {
        path:'/',
        component:Home
       },
        {
        path:'/Home',
        component:Home
       },
        {
        path:'/search',
        component:search
       },
        {
        path:'/searchList',
        component:searchList
       },
       {
        path:'/Reg',
        component:Reg
       },
       {
        path:'/login',
        component:login
       },
       {
         path:'/main',
        component:main
       },
       {
         path:'/setup',
        component:setup
       },
       {
         path:'/kind',
        component:kind
       },
       {
         path:'/login',
        component:login
       },
       {
         path:'/quickReg',
        component:quickReg
       },
       {
         path:'/regnew',
        component:regnew
       },
       {
         path:'/pass',
        component:pass
       },
       {
         path:'/goods',
        component:goods
       },
       {
         path:'/goodscart',
        component:goodscart
       },
       {
        path:'*',
        component:Notfound
       }
       
   ]
   
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  console.log('全局守卫的钩子函数');
  //	to: 目标路由
  //	from: 当前路由
  // next() 跳转  一定要调用

  if(to.path==='/goodscart'){
    if(!sessionStorage.getItem('username')){
      next('/login?path='+to.path)  //实现登录后继续返回跳转登录页面之前的页面
    }
  }
  next()
})
export default router
